home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2001 May / SGI IRIX Base Documentation 2001 May.iso / usr / share / catman / p_man / cat3 / perl5 / Safe.z / Safe
Encoding:
Text File  |  1998-10-30  |  15.4 KB  |  463 lines

  1.  
  2.  
  3.  
  4. SSSSaaaaffffeeee((((3333))))                                                                SSSSaaaaffffeeee((((3333))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      Safe - Compile and execute code in restricted compartments
  10.  
  11. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.        use Safe;
  13.  
  14.        $compartment = new Safe;
  15.  
  16.        $compartment->permit(qw(time sort :browse));
  17.  
  18.        $result = $compartment->reval($unsafe_code);
  19.  
  20.  
  21. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  22.      The Safe extension module allows the creation of compartments in which
  23.      perl code can be evaluated. Each compartment has
  24.  
  25.      a new namespace
  26.              The "root" of the namespace (i.e. "main::") is changed to a
  27.              different package and code evaluated in the compartment cannot
  28.              refer to variables outside this namespace, even with run-time
  29.              glob lookups and other tricks.
  30.  
  31.              Code which is compiled outside the compartment can choose to
  32.              place variables into (or _s_h_a_r_e variables with) the compartment's
  33.              namespace and only that data will be visible to code evaluated in
  34.              the compartment.
  35.  
  36.              By default, the only variables shared with compartments are the
  37.              "underscore" variables $_ and @_ (and, technically, the less
  38.              frequently used %_, the _ filehandle and so on). This is because
  39.              otherwise perl operators which default to $_ will not work and
  40.              neither will the assignment of arguments to @_ on subroutine
  41.              entry.
  42.  
  43.      an operator mask
  44.              Each compartment has an associated "operator mask". Recall that
  45.              perl code is compiled into an internal format before execution.
  46.              Evaluating perl code (e.g. via "eval" or "do 'file'") causes the
  47.              code to be compiled into an internal format and then, provided
  48.              there was no error in the compilation, executed.  Code evaulated
  49.              in a compartment compiles subject to the compartment's operator
  50.              mask. Attempting to evaulate code in a compartment which contains
  51.              a masked operator will cause the compilation to fail with an
  52.              error. The code will not be executed.
  53.  
  54.              The default operator mask for a newly created compartment is the
  55.              ':default' optag.
  56.  
  57.              It is important that you read the _O_p_c_o_d_e(3) module documentation
  58.              for more information, especially for detailed definitions of
  59.              opnames, optags and opsets.
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. SSSSaaaaffffeeee((((3333))))                                                                SSSSaaaaffffeeee((((3333))))
  71.  
  72.  
  73.  
  74.              Since it is only at the compilation stage that the operator mask
  75.              applies, controlled access to potentially unsafe operations can
  76.              be achieved by having a handle to a wrapper subroutine (written
  77.              outside the compartment) placed into the compartment. For
  78.              example,
  79.  
  80.                  $cpt = new Safe;
  81.                  sub wrapper {
  82.                      # vet arguments and perform potentially unsafe operations
  83.                  }
  84.                  $cpt->share('&wrapper');
  85.  
  86.  
  87. WWWWAAAARRRRNNNNIIIINNNNGGGG
  88.      The authors make nnnnoooo wwwwaaaarrrrrrrraaaannnnttttyyyy, implied or otherwise, about the suitability
  89.      of this software for safety or security purposes.
  90.  
  91.      The authors shall not in any case be liable for special, incidental,
  92.      consequential, indirect or other similar damages arising from the use of
  93.      this software.
  94.  
  95.      Your mileage will vary. If in any doubt ddddoooo nnnnooootttt uuuusssseeee iiiitttt.
  96.  
  97.      RRRREEEECCCCEEEENNNNTTTT CCCCHHHHAAAANNNNGGGGEEEESSSS
  98.  
  99.      The interface to the Safe module has changed quite dramatically since
  100.      version 1 (as supplied with Perl5.002). Study these pages carefully if
  101.      you have code written to use Safe version 1 because you will need to
  102.      makes changes.
  103.  
  104.      MMMMeeeetttthhhhooooddddssss iiiinnnn ccccllllaaaassssssss SSSSaaaaffffeeee
  105.  
  106.      To create a new compartment, use
  107.  
  108.          $cpt = new Safe;
  109.  
  110.      Optional argument is (NAMESPACE), where NAMESPACE is the root namespace
  111.      to use for the compartment (defaults to "Safe::Root0", incremented for
  112.      each new compartment).
  113.  
  114.      Note that version 1.00 of the Safe module supported a second optional
  115.      parameter, MASK.  That functionality has been withdrawn pending deeper
  116.      consideration. Use the permit and deny methods described below.
  117.  
  118.      The following methods can then be used on the compartment object returned
  119.      by the above constructor. The object argument is implicit in each case.
  120.  
  121.      permit (OP, ...)
  122.              Permit the listed operators to be used when compiling code in the
  123.              compartment (in _a_d_d_i_t_i_o_n to any operators already permitted).
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. SSSSaaaaffffeeee((((3333))))                                                                SSSSaaaaffffeeee((((3333))))
  137.  
  138.  
  139.  
  140.      permit_only (OP, ...)
  141.              Permit _o_n_l_y the listed operators to be used when compiling code
  142.              in the compartment (_n_o other operators are permitted).
  143.  
  144.      deny (OP, ...)
  145.              Deny the listed operators from being used when compiling code in
  146.              the compartment (other operators may still be permitted).
  147.  
  148.      deny_only (OP, ...)
  149.              Deny _o_n_l_y the listed operators from being used when compiling
  150.              code in the compartment (_a_l_l other operators will be permitted).
  151.  
  152.      trap (OP, ...)
  153.  
  154.      untrap (OP, ...)
  155.              The trap and untrap methods are synonyms for deny and permit
  156.              respectfully.
  157.  
  158.      share (NAME, ...)
  159.              This shares the _v_a_r_i_a_b_l_e(s) in the argument list with the
  160.              compartment.  This is almost identical to exporting variables
  161.              using the the _E_x_p_o_r_t_e_r(_3) manpage module.
  162.  
  163.              Each NAME must be the nnnnaaaammmmeeee of a variable, typically with the
  164.              leading type identifier included. A bareword is treated as a
  165.              function name.
  166.  
  167.              Examples of legal names are '$foo' for a scalar, '@foo' for an
  168.              array, '%foo' for a hash, '&foo' or 'foo' for a subroutine and
  169.              '*foo' for a glob (i.e.  all symbol table entries associated with
  170.              "foo", including scalar, array, hash, sub and filehandle).
  171.  
  172.              Each NAME is assumed to be in the calling package. See share_from
  173.              for an alternative method (which share uses).
  174.  
  175.      share_from (PACKAGE, ARRAYREF)
  176.              This method is similar to _s_h_a_r_e() but allows you to explicitly
  177.              name the package that symbols should be shared from. The symbol
  178.              names (including type characters) are supplied as an array
  179.              reference.
  180.  
  181.                  $safe->share_from('main', [ '$foo', '%bar', 'func' ]);
  182.  
  183.  
  184.      varglob (VARNAME)
  185.              This returns a glob reference for the symbol table entry of
  186.              VARNAME in the package of the compartment. VARNAME must be the
  187.              nnnnaaaammmmeeee of a variable without any leading type marker. For example,
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. SSSSaaaaffffeeee((((3333))))                                                                SSSSaaaaffffeeee((((3333))))
  203.  
  204.  
  205.  
  206.                  $cpt = new Safe 'Root';
  207.                  $Root::foo = "Hello world";
  208.                  # Equivalent version which doesn't need to know $cpt's package name:
  209.                  ${$cpt->varglob('foo')} = "Hello world";
  210.  
  211.  
  212.      reval (STRING)
  213.              This evaluates STRING as perl code inside the compartment.
  214.  
  215.              The code can only see the compartment's namespace (as returned by
  216.              the rrrrooooooootttt method). The compartment's root package appears to be
  217.              the main:: package to the code inside the compartment.
  218.  
  219.              Any attempt by the code in STRING to use an operator which is not
  220.              permitted by the compartment will cause an error (at run-time of
  221.              the main program but at compile-time for the code in STRING).
  222.              The error is of the form "%s trapped by operation mask
  223.              operation...".
  224.  
  225.              If an operation is trapped in this way, then the code in STRING
  226.              will not be executed. If such a trapped operation occurs or any
  227.              other compile-time or return error, then $@ is set to the error
  228.              message, just as with an _e_v_a_l().
  229.  
  230.              If there is no error, then the method returns the value of the
  231.              last expression evaluated, or a return statement may be used,
  232.              just as with subroutines and eeeevvvvaaaallll(((()))). The context (list or scalar)
  233.              is determined by the caller as usual.
  234.  
  235.              This behaviour differs from the beta distribution of the Safe
  236.              extension where earlier versions of perl made it hard to mimic
  237.              the return behaviour of the _e_v_a_l() command and the context was
  238.              always scalar.
  239.  
  240.              Some points to note:
  241.  
  242.              If the entereval op is permitted then the code can use eval "..."
  243.              to 'hide' code which might use denied ops. This is not a major
  244.              problem since when the code tries to execute the eval it will
  245.              fail because the opmask is still in effect. However this
  246.              technique would allow clever, and possibly harmful, code to
  247.              'probe' the boundaries of what is possible.
  248.  
  249.              Any string eval which is executed by code executing in a
  250.              compartment, or by code called from code executing in a
  251.              compartment, will be eval'd in the namespace of the compartment.
  252.              This is potentially a serious problem.
  253.  
  254.              Consider a function _f_o_o() in package pkg compiled outside a
  255.              compartment but shared with it. Assume the compartment has a root
  256.              package called 'Root'. If _f_o_o() contains an eval statement like
  257.              eval '$foo = 1' then, normally, $pkg::foo will be set to 1.  If
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. SSSSaaaaffffeeee((((3333))))                                                                SSSSaaaaffffeeee((((3333))))
  269.  
  270.  
  271.  
  272.              _f_o_o() is called from the compartment (by whatever means) then
  273.              instead of setting $pkg::foo, the eval will actually set
  274.              $Root::pkg::foo.
  275.  
  276.              This can easily be demonstrated by using a module, such as the
  277.              Socket module, which uses eval "..." as part of an AUTOLOAD
  278.              function. You can 'use' the module outside the compartment and
  279.              share an (autoloaded) function with the compartment. If an
  280.              autoload is triggered by code in the compartment, or by any code
  281.              anywhere that is called by any means from the compartment, then
  282.              the eval in the Socket module's AUTOLOAD function happens in the
  283.              namespace of the compartment. Any variables created or used by
  284.              the eval'd code are now under the control of the code in the
  285.              compartment.
  286.  
  287.              A similar effect applies to _a_l_l runtime symbol lookups in code
  288.              called from a compartment but not compiled within it.
  289.  
  290.      rdo (FILENAME)
  291.              This evaluates the contents of file FILENAME inside the
  292.              compartment.  See above documentation on the rrrreeeevvvvaaaallll method for
  293.              further details.
  294.  
  295.      root (NAMESPACE)
  296.              This method returns the name of the package that is the root of
  297.              the compartment's namespace.
  298.  
  299.              Note that this behaviour differs from version 1.00 of the Safe
  300.              module where the root module could be used to change the
  301.              namespace. That functionality has been withdrawn pending deeper
  302.              consideration.
  303.  
  304.      mask (MASK)
  305.              This is a get-or-set method for the compartment's operator mask.
  306.  
  307.              With no MASK argument present, it returns the current operator
  308.              mask of the compartment.
  309.  
  310.              With the MASK argument present, it sets the operator mask for the
  311.              compartment (equivalent to calling the deny_only method).
  312.  
  313.      SSSSoooommmmeeee SSSSaaaaffffeeeettttyyyy IIIIssssssssuuuueeeessss
  314.  
  315.      This section is currently just an outline of some of the things code in a
  316.      compartment might do (intentionally or unintentionally) which can have an
  317.      effect outside the compartment.
  318.  
  319.      Memory  Consuming all (or nearly all) available memory.
  320.  
  321.      CPU     Causing infinite loops etc.
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. SSSSaaaaffffeeee((((3333))))                                                                SSSSaaaaffffeeee((((3333))))
  335.  
  336.  
  337.  
  338.      Snooping
  339.              Copying private information out of your system. Even something as
  340.              simple as your user name is of value to others. Much useful
  341.              information could be gleaned from your environment variables for
  342.              example.
  343.  
  344.      Signals Causing signals (especially SIGFPE and SIGALARM) to affect your
  345.              process.
  346.  
  347.              Setting up a signal handler will need to be carefully considered
  348.              and controlled.  What mask is in effect when a signal handler
  349.              gets called?  If a user can get an imported function to get an
  350.              exception and call the user's signal handler, does that user's
  351.              restricted mask get re-instated before the handler is called?
  352.              Does an imported handler get called with its original mask or the
  353.              user's one?
  354.  
  355.      State Changes
  356.              Ops such as chdir obviously effect the process as a whole and not
  357.              just the code in the compartment. Ops such as rand and srand have
  358.              a similar but more subtle effect.
  359.  
  360.      AAAAUUUUTTTTHHHHOOOORRRR
  361.  
  362.      Originally designed and implemented by Malcolm Beattie,
  363.      mbeattie@sable.ox.ac.uk.
  364.  
  365.      Reworked to use the Opcode module and other changes added by Tim Bunce
  366.      <_T_i_m._B_u_n_c_e@_i_g._c_o._u_k>.
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. SSSSaaaaffffeeee((((3333))))                                                                SSSSaaaaffffeeee((((3333))))
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.                                                                         PPPPaaaaggggeeee 7777
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.